package com.jianghu;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.jianghu.helpers.GetLineFromtxt;
import com.jianghu.helpers.ImageAndTextListAdapter;
import com.jianghu.helpers.TrendsShow;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class FriendsTrendsManual extends Activity {

	private ListView listView;
	private boolean surplus = true;
	private int count = 0;
	private Thread mThread;
	private ProgressBar progressBar;
	private TextView txtMore;

	final Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				update();
				break;
			}
			super.handleMessage(msg);
		}

		void update() {
			// conncect Server for new trends,test by toast
			Toast.makeText(FriendsTrendsManual.this, "有新动态，请点击刷新获取!",
					Toast.LENGTH_LONG).show();
		}
	};
	Timer timer = new Timer();
	TimerTask task = new TimerTask() {
		public void run() {

		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friendtrends);
		timer.schedule(task, 1000 * 4, 1000 * 3);
		ImageButton home = (ImageButton) findViewById(R.id.homebuttonofusertrends);
		home.setOnClickListener(new homeClickListener());

		ImageButton refresh = (ImageButton) findViewById(R.id.refreshbuttonofusertrends);
		refresh.setOnClickListener(new refreshClickListener());

		listView = (ListView) findViewById(R.id.listviewOfFriendstrends);
		final List<TrendsShow> dataArray = new ArrayList<TrendsShow>();
		listView.setCacheColorHint(0);

		for (int i = 0; i < 20; i++) {

			String str = GetLineFromtxt.readAppointedLineNumber(this, i + 1);
			if (str == null) {
				surplus = false;
				break;
			}
			count++;
			String imageUrl = "http://t3.baidu.com/it/u=3744283480,376574923&fm=0&gp=0.jpg";
			String username = "时尚英语";
			String time = i + "小时前";
			String contents = str;
			TrendsShow trendsShow = new TrendsShow(imageUrl, username, time,
					contents);
			dataArray.add(trendsShow);
		}

		final ImageAndTextListAdapter listItemAdapter = new ImageAndTextListAdapter(
				this, dataArray, listView);
		Log.v("dataarray", dataArray.size()+"");
		final View loadingLayout = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.footer_listview_user_trends, null, false);
		listView.addFooterView(loadingLayout, null, true);
		listView.setAdapter(listItemAdapter);

		
		
		
		
		loadingLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				progressBar = (ProgressBar) loadingLayout
						.findViewById(R.id.progressBarofUserTrends);
				progressBar.setVisibility(View.VISIBLE);
				txtMore = (TextView) loadingLayout
						.findViewById(R.id.moreofusertrends);
				txtMore.setVisibility(View.GONE);
				if (mThread == null || !mThread.isAlive()) {
					mThread = new Thread() {
						@Override
						public void run() {
							try {

								loadRemnantListItem(count);
								Thread.sleep(5000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
						}
					};
					mThread.start();
				}

			}

			private Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					switch (msg.what) {
					case 1:

						listItemAdapter.notifyDataSetChanged();
						if (surplus == false) {

							listView.removeFooterView(loadingLayout);
						} else {
							progressBar = (ProgressBar) loadingLayout
									.findViewById(R.id.progressBarofUserTrends);
							progressBar.setVisibility(View.GONE);
							txtMore = (TextView) loadingLayout
									.findViewById(R.id.moreofusertrends);
							txtMore.setVisibility(View.VISIBLE);
						}
						break;
					default:
						break;
					}
				}
			};

			private void loadRemnantListItem(int totalItemCount) {

				int max = totalItemCount + 20;
				for (int i = totalItemCount; i < max; i++) {

					String str = GetLineFromtxt.readAppointedLineNumber(
							FriendsTrendsManual.this, i + 1);
					if (str == null) {
						surplus = false;
						break;
					}
					count++;
					String imageUrl = "http://t3.baidu.com/it/u=3744283480,376574923&fm=0&gp=0.jpg";
					String username = "冷笑话精选";
					String time = i + "小时前";
					String contents = str;
					TrendsShow trendsShow = new TrendsShow(imageUrl, username,
							time, contents);
					dataArray.add(trendsShow);
				}

			}
		});

	}

	class homeClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(FriendsTrendsManual.this, MainPage.class);
			FriendsTrendsManual.this.startActivity(intent);
		}

	}

	class refreshClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {

		}

	}

	@Override
	protected void onDestroy() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		super.onDestroy();
	}

}